<template>
    <div class="batch_form_container">
        <el-form label-width="0px">
            <el-form-item>
                <span>一行一个，冒号(:)、等号（=）、空格、Tab分割重量和运费, 可从Excel直接复制两列数据</span>
                <p>例如：1:10、1=10、1 10、1  10</p>
            </el-form-item>
            <el-form-item>
                <el-input v-model="batchContent" type="textarea" :rows="6" />
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "BatchForm",
    props: {
        propsData: {
            type: Object,
            default() {
                return {
                    init: true,
                };
            },
        },
    },
    data() {
        return {
            rateList: [],
            batchContent: '',
        };
    },
    watch: {
        propsData: {
            handler() {
                this.init();
            },
            deep: true,
            immediate: true,
        },
    },
    methods: {
        init() {
            const _this = this;
            if (_this.propsData.init) {
                return;
            }
            _this.rateList = _this.propsData.rateList || [];
        },
        async confirm(_component) {
            const _this = this;
            let list = [].concat(_this.rateList);
            let inputDatas = _this.batchContent.split("\n");
            inputDatas.forEach(element => {
                let eleData = element.replace(/：/g, ':');
                const splitChar = [ ":", "=", " ", "\t" ];
                for (const index in splitChar) {
                    let inputIndex = eleData.indexOf(splitChar[index]);
                    if (inputIndex !== -1) {
                        let weight = Number(eleData.substring(0, inputIndex));
                        let freight = Number(eleData.substring(inputIndex + 1));
                        let filter = _this.rateList.filter(item => Number(item.weight) === weight);
                        if (_this.$utils.isNotEmptyList(filter)) {
                            list.forEach(item => {
                                if (Number(item.weight) === weight) {
                                    item.freight = freight;
                                }
                            });
                        } else {
                            // 添加
                            list.push({
                                weight,
                                freight,
                            });
                        }
                        break;
                    }
                }
            });
            let sortedList = list.sort((a, b) => Number(a.weight) - Number(b.weight));
            let rateList = _this.resetIndex(sortedList);
            if (_component) {
                typeof _component.updateRateList === 'function' && _component.updateRateList(rateList);
            }
            return true;
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.express_form_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
